/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 *
 *MpmProjectResourcePoolController
 * 部门审批人资源池
 *
 * 修改纪录
 * 2020-12-25 版本：1.0 lanjianwei 创建。
 * @version 版本：1.0
 * @author 作者：lanjianwei
 * 创建日期：2020-12-25
 */

package com.topscomm.mpm.controller;

import com.topscomm.basic.BasicEntity;
import com.topscomm.cbo.pojo.CboDeptEntity;
import com.topscomm.cbo.service.impl.CboDeptService;
import com.topscomm.mpm.pojo.MpmProjectResourcePoolEntity;
import com.topscomm.pub.util.ConvertUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.mpm.controller.auto.MpmProjectResourcePoolControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: lanjianwei
 * @date: 2020-12-25
 * @description:部门审批人资源池Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectResourcePool")
public class MpmProjectResourcePoolController extends MpmProjectResourcePoolControllerAuto implements IMpmProjectResourcePoolController {

	@Autowired
	private CboDeptService cboDeptService;



	/**
	 * @author: lanjianwei
	 * @date: 2020-12-25
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_str")) {
			String value = paraMap.get("qry_str").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (usercode like '%{0}%' or username like '%{0}%')", value);
		}

		if(paraMap.containsKey("qry_dept")){
			String value = paraMap.get("qry_dept").toString();
			if(!StringUtil.isEmpty(value)){
				sql.appendSingle(" and deptname like '%{0}%'",value);
			}
		}
		if(paraMap.containsKey("qry_projectlevel")){
			String value = paraMap.get("qry_projectlevel").toString();
			if(!StringUtil.isEmpty(value)){
				String[] split = value.split(",");
				if(split.length>1) {
					for (int i = 0; i < split.length; i++) {
						if (i == 0) {
							sql.appendSingle("and (projectlevel like '%{0}%'", split[i]);
						} else if (i == split.length - 1) {
							sql.appendSingle(" or projectlevel like '%{0}%')", split[i]);
						} else {
							sql.appendSingle(" or projectlevel like '%{0}%'", split[i]);
						}
					}
				}else{
					sql.appendSingle("and projectlevel like '%{0}%'",split[0]);
				}
			}
		}
		if(paraMap.containsKey("qry_projecttype")){
			String value = paraMap.get("qry_projecttype").toString();
			if(!StringUtil.isEmpty(value)){
				sql.appendSingle(" and  projecttype in ({0})",value);
			}
		}


		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: lanjianwei
	 * @date: 2020-12-25
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
//		paraMap.put("qry_projecttype",paraMap.get("projecttype"));
		paraMap.put("qry_projectlevel",paraMap.get("projectlevel"));
		return super.refList(paraMap);
	}


	/**
	 * @author: wudaliang
	 * @date: 2020-12-28
	 * @param: paraMap
	 * @description:
	 * @return: com.topscomm.pub.vo.ResponseResult<java.lang.Object>
	 * @modify:
	 */
	@Override
	@PostMapping(value = "/insert")
	public ResponseResult<Object> insert(@RequestBody(required = false)Map<String, Object> paraMap) {
		List<Map<String, Object>> userList = (List<Map<String, Object>>) paraMap.get("userList");
		ArrayList<BasicEntity> entityList = new ArrayList<>();
		for(Map<String,Object> user :userList){
			//创建审批人资源池对象
			MpmProjectResourcePoolEntity entity = new MpmProjectResourcePoolEntity();
			entity.setUsercode(ConvertUtil.convertToString(user.get("code")));
			entity.setUsername(ConvertUtil.convertToString(user.get("name")));
			List<String> projectlevelList = (List<String>)paraMap.get("projectlevel");
			Collections.sort(projectlevelList);
			StringBuffer string = new StringBuffer();
		    for(String s:projectlevelList){
		    	string.append(s+",");
			}
			String str = string.substring(0, string.lastIndexOf(","));
			entity.setProjectlevel(str);
			entity.setProjecttype(ConvertUtil.convertToString(paraMap.get("projecttype")));
			//用户参照选择，平台传过来的是deptid，需要根据deptid查出deptcode
			CboDeptEntity cboDeptEntity = cboDeptService
				.queryById(ConvertUtil.convertToString(user.get("deptid")));
			entity.setDeptcode(cboDeptEntity.getCode());
			entity.setDeptname(ConvertUtil.convertToString(paraMap.get("deptname")));
			if(paraMap.containsKey("description")){
				entity.setDescription(ConvertUtil.convertToString(paraMap.get("description")));
			}
			entityList.add(entity);
		}
		this.service.insertBatch(entityList);
		return ResponseResult.ok(paraMap);
	}


	@Override
	protected void beforeUpdate(Map<String, Object> dataMap, Map<String, Object> paraMap)
		throws Exception {
		if(dataMap.containsKey("projectlevel")){
			List<String> list = (List<String>) dataMap.get("projectlevel");
			StringBuffer string = new StringBuffer();
			for(String a:list){
				string.append(a+",");
			}
			String str = string.substring(0, string.lastIndexOf(","));
			dataMap.remove("projectlevel");
			if(!StringUtil.isEmpty(list.toString())){
				dataMap.put("projectlevel", str);
			}
		}
	}
}

